package com.hruiworks.campusgroup.quartz.factory.trigger;

import com.hruiworks.campusgroup.pojo.entity.Group;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.quartz.common.QuartzGroupUtils;
import com.hruiworks.campusgroup.quartz.common.QuartzNameUtils;
import com.hruiworks.campusgroup.quartz.factory.trigger.TriggerFactory;
import com.hruiworks.usercheck.util.CronUtils;
import org.quartz.Trigger;

import java.time.LocalDateTime;
import java.time.temporal.TemporalUnit;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

public class GroupStartNoticeTriggerFactory extends TriggerFactory {

    /**
     * 创建活动开始提前通知触发器
     *
     * @param user           用户
     * @param group          活动团队
     * @param time           时间
     * @param timeBeforeHand 提前时间
     * @param unit           单位
     * @return 返回触发器
     */
    public final Trigger getTrigger(User user, Group group, LocalDateTime time, Long timeBeforeHand, TemporalUnit unit) {
        String triggerGroup = user.getId().toString();
        String triggerName = group.getId().toString();
        LocalDateTime noticeTime = time.minus(timeBeforeHand, unit);
        String cron = CronUtils.onlyOnce(noticeTime);

        return newTrigger()
                .withIdentity(triggerName, triggerGroup)
                .withSchedule(cronSchedule(cron))
                .build();
    }

    /**
     * 创建活动开始提前通知触发器
     *
     * @param user         用户
     * @param group        活动团队
     * @param time         时间
     * @return 返回触发器
     */
    public final Trigger getTrigger(User user, Group group, LocalDateTime time) {
        String triggerGroup = QuartzGroupUtils.groupStartNotice(user);
        String triggerName = QuartzNameUtils.groupStartNotice(group);
        String cron = CronUtils.onlyOnce(time);
        return newTrigger()
                .withIdentity(triggerName, triggerGroup)
                .withSchedule(cronSchedule(cron))
                .build();
    }

}
